import axios, { ResDataType } from './ajax'
// import type {ResDataType} from './ajax'

type SearchOption = {
  keyword: string
  isStar: boolean
  isDelete: boolean
  page: number
  pageSize: number
  // page
  // pageSize
  // ....
}

// 获取单个问卷信息
export async function getQuestionService(id: string): Promise<ResDataType> {
  const url = `/api/question/${id}`
  // 发起get请求
  const data = axios.get(url)
  return data
}

// 创建问卷
export async function createQuestionService(): Promise<ResDataType> {
  const url = '/api/question'
  const data = (await axios.post(url)) as ResDataType
  return data
}

// 获取(查询)问卷列表
export async function getQuestionListService(opt: Partial<SearchOption>): Promise<ResDataType> {
  const url = '/api/question' //相当于拼接?a=10&b=20
  const data = (await axios.get(url, { params: opt })) as ResDataType
  return data
}

// 更新单个问卷数据
export async function updateQuestionListService(
  id: string,
  opt: { [key: string]: any }
): Promise<ResDataType> {
  // Promise<ResDataType> 它将 data 作为 Promise 的结果返回
  const url = `/api/question/${id}`
  const data = (await axios.patch(url, opt)) as ResDataType
  return data
}

// 复制单个问卷数据
export async function duplicateQuestionListService(id: string): Promise<ResDataType> {
  // Promise<ResDataType> 它将 data 作为 Promise 的结果返回
  const url = `/api/question/duplicate/${id}`
  const data = (await axios.post(url)) as ResDataType
  return data
}

// 批量删除问卷数据
export async function deleteQuestionService(ids: string[]): Promise<ResDataType> {
  // Promise<ResDataType> 它将 data 作为 Promise 的结果返回
  const url = `/api/question`
  const data = (await axios.delete(url, { data: { ids } })) as ResDataType
  return data
}
